<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>迭代器的使用</title>
    <meta name="description" content="stl的容器库常用模式就是将容器、迭代器和算法的进行分离，容器专于存储，迭代器负责枚举，这样互相独立好处多多。因此TBOX也借鉴了这种模式，不同的是没用模板，仅仅用了c语言来实现。容器库里面的大部分容器都是继承自迭代器的，所以迭代起来相当的方便。下面先看个迭代器使用的例子：">

    
    <meta name="keywords" content="tbox,迭代器,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2016/02/04/iterator/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>迭代器的使用</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2016-02-04
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#tbox" title="Category: tbox" rel="category">tbox</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a-->
        <a href="/cn/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a>&nbsp;
    
        <!--a href="/tag/#%E8%BF%AD%E4%BB%A3%E5%99%A8" title="Tag: 迭代器" rel="tag">迭代器</a-->
        <a href="/cn/tag/#迭代器" title="Tag: 迭代器" rel="tag">迭代器</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>stl的容器库常用模式就是将容器、迭代器和算法的进行分离，容器专于存储，迭代器负责枚举，这样互相独立好处多多。</p>

<p>因此TBOX也借鉴了这种模式，不同的是没用模板，仅仅用了c语言来实现。容器库里面的大部分容器都是继承自迭代器的，所以迭代起来相当的方便。</p>

<p>下面先看个迭代器使用的例子：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 初始化一个双向链表，元素类型为tb_long_t， 满256个元素自动增长
</span>    <span class="n">tb_list_ref_t</span> <span class="n">list</span> <span class="o">=</span> <span class="n">tb_list_init</span><span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="n">tb_item_func_long</span><span class="p">());</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">list</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 插入一些元素
</span>        <span class="n">tb_list_insert_tail</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">1</span><span class="p">);</span>
        <span class="n">tb_list_insert_tail</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">2</span><span class="p">);</span>
        <span class="n">tb_list_insert_tail</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">3</span><span class="p">);</span>
        <span class="n">tb_list_insert_tail</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">4</span><span class="p">);</span>
        <span class="n">tb_list_insert_tail</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="p">(</span><span class="n">tb_cpointer_t</span><span class="p">)</span><span class="mi">5</span><span class="p">);</span>
        
        <span class="c1">// 迭代遍历所有元素
</span>        <span class="n">tb_for_all</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">list</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"item: %ld"</span><span class="p">,</span> <span class="n">item</span><span class="p">);</span>
        <span class="p">}</span>
        
        <span class="c1">// 迭代遍历所有 &gt; 3 的元素
</span>        <span class="n">tb_for_all_if</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">list</span><span class="p">,</span> <span class="n">item</span> <span class="o">&gt;</span> <span class="mi">3</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"item: %ld"</span><span class="p">,</span> <span class="n">item</span><span class="p">);</span>
        <span class="p">}</span>
        
        <span class="c1">// 反向迭代遍历所有元素，注：只有支持反向迭代的容器，才行，例如单链tb_single_list_t就不行
</span>        <span class="n">tb_rfor_all</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">list</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"item: %ld"</span><span class="p">,</span> <span class="n">item</span><span class="p">);</span>
        <span class="p">}</span>
        
        <span class="c1">// 反向迭代遍历所有 &gt; 3 的元素
</span>        <span class="n">tb_rfor_all_if</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">list</span><span class="p">,</span> <span class="n">item</span> <span class="o">&gt;</span> <span class="mi">3</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"item: %ld"</span><span class="p">,</span> <span class="n">item</span><span class="p">);</span>
        <span class="p">}</span>
        
        <span class="c1">// 迭代遍历部分元素，这里认为传入迭代的头部和尾部，进行遍历所有
</span>        <span class="n">tb_for</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">tb_iterator_head</span><span class="p">(</span><span class="n">list</span><span class="p">),</span> <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="n">list</span><span class="p">),</span> <span class="n">list</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"item: %ld"</span><span class="p">,</span> <span class="n">item</span><span class="p">);</span>
        <span class="p">}</span>
        
        <span class="c1">// 退出链表
</span>        <span class="n">tb_list_exit</span><span class="p">(</span><span class="n">list</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<p>怎么样简单吧，其实这里的 tb_for_all 是一个宏，如果把它展开的话，其实也可以这样遍历只不过看上去繁琐些，但是更加灵活：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 获取list的头部索引
</span>    <span class="n">tb_size_t</span> <span class="n">itor</span> <span class="o">=</span> <span class="n">tb_iterator_head</span><span class="p">(</span><span class="n">list</span><span class="p">);</span>

    <span class="c1">// 获取list的尾部索引
</span>    <span class="n">tb_size_t</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="n">list</span><span class="p">);</span>

    <span class="c1">// 进行遍历，这里没去删除元素，所以不需要实时更新tail的值
</span>    <span class="k">for</span> <span class="p">(;</span> <span class="n">itor</span> <span class="o">!=</span> <span class="n">tail</span><span class="p">;</span> <span class="n">itor</span> <span class="o">=</span> <span class="n">tb_iterator_next</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">itor</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="c1">// 获取索引itor对应的元素
</span>        <span class="n">tb_long_t</span> <span class="n">item</span> <span class="o">=</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">)</span><span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">itor</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="c1">// 进行遍历，并且删除元素，需要更新tail， 所以这个tb_for 就办不到了
</span>    <span class="c1">// tb_for为了效率，不会去更新tail的值
</span>    <span class="k">while</span> <span class="p">(</span><span class="n">itor</span> <span class="o">!=</span> <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">itor</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="c1">// 获取索引itor对应的元素
</span>        <span class="n">tb_long_t</span> <span class="n">item</span> <span class="o">=</span> <span class="p">(</span><span class="n">tb_long_t</span><span class="p">)</span><span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">itor</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">item</span> <span class="o">&gt;</span> <span class="mi">3</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="c1">// 先保存下一个索引，避免删除当前元素后itor变为无效索引
</span>            <span class="n">tb_size_t</span> <span class="n">next</span> <span class="o">=</span> <span class="n">tb_iterator_next</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">itor</span><span class="p">);</span>
            
            <span class="c1">// 使用迭代器删除
</span>            <span class="n">tb_iterator_remove</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">itor</span><span class="p">);</span>
            
            <span class="c1">// 或者使用容器删除
</span>    <span class="c1">//      tb_list_remove(list, itor);
</span>
            <span class="c1">// 继续下一个
</span>            <span class="n">itor</span> <span class="o">=</span> <span class="n">next</span><span class="p">;</span>
            <span class="k">continue</span> <span class="p">;</span>
        <span class="p">}</span>
        
        <span class="c1">// 继续下一个
</span>        <span class="n">itor</span> <span class="o">=</span> <span class="n">tb_iterator_next</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">itor</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<p>这种方式其实也已经相当方便了，如果觉得上面的删除比较繁琐的话，可以使用算法库提供的remove函数来进行遍历删除，并且更加高效。</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 回调函数
</span>    <span class="n">tb_long_t</span> <span class="n">tb_list_item_func</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">item</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">priv</span><span class="p">);</span>
    <span class="p">{</span>
        <span class="c1">// 如果 &gt; 3 就删除它
</span>        <span class="k">if</span> <span class="p">((</span><span class="n">tb_long_t</span><span class="p">)</span><span class="n">item</span> <span class="o">&gt;</span> <span class="mi">3</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="cm">/* 标记这个元素为删除
             * 
             * 注：
             * 这个时候容器内部还没有真正的删除它，里面做了些优化
             * 来一次性删除一块连续的元素，这样效率会高好多
             *
             * 这种删除模式，是最快速的，尤其是对tb_vector_t这种有连续内存的容器，更为高效
             * 避免了每删除一个元素，都要进行一遍tb_memmov内存的搬运
             *
             * 名字类似，vector的遍历删除使用：tb_vector_walk
             */</span>
            <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
        <span class="p">}</span>
        
        <span class="c1">// 返回1则继续下一个，返回-1则中断遍历
</span>        <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="c1">// 遍历所有元素，并对每个元素调用回调函数
</span>    <span class="n">tb_remove_if</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">tb_list_item_func</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">);</span>
</code></pre>
</div>

<p>还有一种遍历方式，就是使用算法库的tb_walk函数，这个和 tb_list_walk类似，但不提供删除功能。
主要应用在通用化，模块化复杂遍历代码的地方：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="n">tb_bool_t</span> <span class="nf">tb_walk_item_func</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_pointer_t</span> <span class="n">item</span><span class="p">,</span> <span class="n">tb_pointer_t</span> <span class="n">priv</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// ...
</span>    <span class="p">}</span>

    <span class="c1">// 遍历所有
</span>    <span class="n">tb_walk_all</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">tb_walk_item_func</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">);</span>

    <span class="c1">// 反向遍历所有
</span>    <span class="n">tb_rwalk_all</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">tb_walk_item_func</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">);</span>

    <span class="c1">// 通过迭代器索引，局部遍历
</span>    <span class="n">tb_walk</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">tb_iterator_head</span><span class="p">(</span><span class="n">list</span><span class="p">),</span> <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="n">list</span><span class="p">),</span> <span class="n">tb_walk_item_func</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">);</span>

    <span class="c1">// 反向通过迭代器索引，局部遍历
</span>    <span class="n">tb_rwalk</span><span class="p">(</span><span class="n">list</span><span class="p">,</span> <span class="n">tb_iterator_head</span><span class="p">(</span><span class="n">list</span><span class="p">),</span> <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="n">list</span><span class="p">),</span> <span class="n">tb_walk_item_func</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">);</span>
</code></pre>
</div>

<p>总结下：</p>

<ol>
  <li>tb_for系列：用于小代码块的简单逻辑遍历</li>
  <li>直接使用迭代器：用于小代码块复杂逻辑遍历</li>
  <li>容器的tb_xxx_walk：用于复杂代码块、高效删除元素时的遍历</li>
  <li>tb_walk系列：用于复杂代码块，不需要删除时的遍历</li>
</ol>

<p>迭代器主要由如下几种访问模式，不同容器支持的力度不一样：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">/// the iterator mode type
</span>    <span class="k">typedef</span> <span class="k">enum</span> <span class="n">__tb_iterator_mode_t</span>
    <span class="p">{</span>
        <span class="n">TB_ITERATOR_MODE_FORWARD</span>        <span class="o">=</span> <span class="mi">1</span>     <span class="c1">//!&lt; 前向遍历迭代器，大部分容器都支持
</span>    <span class="p">,</span>   <span class="n">TB_ITERATOR_MODE_REVERSE</span>        <span class="o">=</span> <span class="mi">2</span>     <span class="c1">//!&lt; 反向向遍历迭代器, 单链不支持
</span>    <span class="p">,</span>   <span class="n">TB_ITERATOR_MODE_RACCESS</span>        <span class="o">=</span> <span class="mi">4</span>     <span class="c1">//!&lt; 随机访问迭代器，链表、队列、堆栈等不支持，最常用就是vector
</span>    <span class="p">,</span>   <span class="n">TB_ITERATOR_MODE_MUTABLE</span>        <span class="o">=</span> <span class="mi">8</span>     <span class="c1">//!&lt; 易变的迭代器，同一个迭代器索引的值有可能因为删除某个元素而改变，例如：vector
</span>    <span class="p">,</span>   <span class="n">TB_ITERATOR_MODE_READONLY</span>       <span class="o">=</span> <span class="mi">16</span>    <span class="c1">//!&lt; 只读访问迭代器，不提供删除、替换等操作
</span>
    <span class="p">}</span><span class="n">tb_iterator_mode_t</span><span class="p">;</span>
<span class="err">常用的一些迭代器接口：</span>

    <span class="c1">// 获取迭代器访问模式
</span>    <span class="n">tb_size_t</span>       <span class="n">tb_iterator_mode</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">);</span>

    <span class="c1">// 获取迭代器对应容器的元素总数
</span>    <span class="n">tb_size_t</span>       <span class="n">tb_iterator_size</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">);</span>

    <span class="c1">// 获取迭代器的头部索引
</span>    <span class="n">tb_size_t</span>       <span class="n">tb_iterator_head</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">);</span>

    <span class="c1">// 获取迭代器的最后一个元素的索引
</span>    <span class="n">tb_size_t</span>       <span class="n">tb_iterator_last</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">);</span>

    <span class="c1">// 获取迭代器的尾部索引，不指向实际元素，一般用于判断结束
</span>    <span class="n">tb_size_t</span>       <span class="n">tb_iterator_tail</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">);</span>

    <span class="c1">// 获取迭代器的先前一个元素的索引
</span>    <span class="n">tb_size_t</span>       <span class="n">tb_iterator_prev</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">itor</span><span class="p">);</span>

    <span class="c1">// 获取迭代器的下一个元素的索引
</span>    <span class="n">tb_size_t</span>       <span class="n">tb_iterator_next</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">itor</span><span class="p">);</span>

    <span class="c1">// 获取迭代器索引指向的元素
</span>    <span class="n">tb_pointer_t</span>    <span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">itor</span><span class="p">);</span>

    <span class="c1">// 删除迭代器索引指向的元素
</span>    <span class="n">tb_void_t</span>       <span class="n">tb_iterator_remove</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">itor</span><span class="p">);</span>

    <span class="c1">// 复制迭代器索引指向的元素
</span>    <span class="n">tb_void_t</span>       <span class="n">tb_iterator_copy</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">itor</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">item</span><span class="p">);</span>

    <span class="c1">// 比较迭代器索引指向的两个元素
</span>    <span class="n">tb_long_t</span>       <span class="n">tb_iterator_comp</span><span class="p">(</span><span class="n">tb_iterator_ref_t</span> <span class="n">iterator</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">ltem</span><span class="p">,</span> <span class="n">tb_cpointer_t</span> <span class="n">rtem</span><span class="p">);</span>
</code></pre>
</div>

<p>你也可以将常用的原始数组，迭代器化，来支持迭代，并用于所有算法：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 根据一个tb_long_t[]数组，生成迭代器, size 为数组元素个数
</span>    <span class="n">tb_iterator_t</span>   <span class="n">tb_iterator_init_long</span><span class="p">(</span><span class="n">tb_long_t</span><span class="o">*</span> <span class="n">data</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">size</span><span class="p">);</span>

    <span class="c1">// 根据一个tb_size_t[]数组，生成迭代器, size 为数组元素个数
</span>    <span class="n">tb_iterator_t</span>   <span class="n">tb_iterator_init_size</span><span class="p">(</span><span class="n">tb_size_t</span><span class="o">*</span> <span class="n">data</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">size</span><span class="p">);</span>

    <span class="c1">// 根据一个tb_char_t*[]字符串数组，生成迭代器, size 为数组元素个数
</span>    <span class="n">tb_iterator_t</span>   <span class="n">tb_iterator_init_str</span><span class="p">(</span><span class="n">tb_char_t</span><span class="o">**</span> <span class="n">data</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">size</span><span class="p">);</span>

    <span class="c1">// 根据一个tb_pointer_t[]指针数组，生成迭代器, size 为数组元素个数
</span>    <span class="n">tb_iterator_t</span>   <span class="n">tb_iterator_init_ptr</span><span class="p">(</span><span class="n">tb_pointer_t</span><span class="o">*</span> <span class="n">data</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">size</span><span class="p">);</span>

    <span class="c1">// 根据一个tb_struct_xxxx_t[]结构体数组，生成迭代器, size 为数组元素个数, step == sizeof(tb_struct_xxxx_t)
</span>    <span class="n">tb_iterator_t</span>   <span class="n">tb_iterator_init_mem</span><span class="p">(</span><span class="n">tb_pointer_t</span> <span class="n">data</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">size</span><span class="p">,</span> <span class="n">tb_size_t</span> <span class="n">step</span><span class="p">);</span>
</code></pre>
</div>

        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        

        
        
            
        
            
            
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
                
                    
                
                    
                
            
            
        
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2016/02/04/container-hash/">哈希容器的使用</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2016/02/04/memory-check/">内存检测</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
